home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / Common / src / dxutil.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-31  |  23.1 KB  |  720 lines

  1. //-----------------------------------------------------------------------------
  2. // File: DXUtil.cpp
  3. //
  4. // Desc: Shortcut macros and functions for using DX objects
  5. //
  6. //
  7. // Copyright (c) 1997-2001 Microsoft Corporation. All rights reserved
  8. //-----------------------------------------------------------------------------
  9. #define STRICT
  10. #include <windows.h>
  11. #include <mmsystem.h>
  12. #include <tchar.h>
  13. #include <stdio.h> 
  14. #include <stdarg.h>
  15. #include "DXUtil.h"
  16.  
  17.  
  18.  
  19.  
  20. //-----------------------------------------------------------------------------
  21. // Name: DXUtil_GetDXSDKMediaPath()
  22. // Desc: Returns the DirectX SDK media path
  23. //-----------------------------------------------------------------------------
  24. const TCHAR* DXUtil_GetDXSDKMediaPath()
  25. {
  26.     static TCHAR strNull[2] = _T("");
  27.     static TCHAR strPath[MAX_PATH];
  28.     DWORD dwType;
  29.     DWORD dwSize = MAX_PATH;
  30.     HKEY  hKey;
  31.  
  32.     // Open the appropriate registry key
  33.     LONG lResult = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  34.                                 _T("Software\\Microsoft\\DirectX SDK"),
  35.                                 0, KEY_READ, &hKey );
  36.     if( ERROR_SUCCESS != lResult )
  37.         return strNull;
  38.  
  39.     lResult = RegQueryValueEx( hKey, _T("DX81SDK Samples Path"), NULL,
  40.                               &dwType, (BYTE*)strPath, &dwSize );
  41.     RegCloseKey( hKey );
  42.  
  43.     if( ERROR_SUCCESS != lResult )
  44.         return strNull;
  45.  
  46.     _tcscat( strPath, _T("\\Media\\") );
  47.  
  48.     return strPath;
  49. }
  50.  
  51.  
  52.  
  53.  
  54. //-----------------------------------------------------------------------------
  55. // Name: DXUtil_FindMediaFile()
  56. // Desc: Returns a valid path to a DXSDK media file
  57. //-----------------------------------------------------------------------------
  58. HRESULT DXUtil_FindMediaFile( TCHAR* strPath, TCHAR* strFilename )
  59. {
  60.     HANDLE file;
  61.     TCHAR strFullPath[1024];
  62.     TCHAR *strShortName;
  63.     DWORD cchPath;
  64.  
  65.     if( NULL==strFilename || NULL==strPath )
  66.         return E_INVALIDARG;
  67.  
  68.     // Build full path name from strFileName (strShortName will be just the leaf filename)
  69.     cchPath = GetFullPathName(strFilename, sizeof(strFullPath)/sizeof(TCHAR), strFullPath, &strShortName);
  70.     if ((cchPath == 0) || (sizeof(strFullPath)/sizeof(TCHAR) <= cchPath))
  71.         return E_FAIL;
  72.  
  73.     // first try to find the filename given a full path
  74.     file = CreateFile( strFullPath, GENERIC_READ, FILE_SHARE_READ, NULL, 
  75.                        OPEN_EXISTING, 0, NULL );
  76.     if( INVALID_HANDLE_VALUE != file )
  77.     {
  78.         _tcscpy( strPath, strFullPath );
  79.         CloseHandle( file );
  80.         return S_OK;
  81.     }
  82.     
  83.     // next try to find the filename in the current working directory (path stripped)
  84.     file = CreateFile( strShortName, GENERIC_READ, FILE_SHARE_READ, NULL, 
  85.                        OPEN_EXISTING, 0, NULL );
  86.     if( INVALID_HANDLE_VALUE != file )
  87.     {
  88.         _tcscpy( strPath, strShortName );
  89.         CloseHandle( file );
  90.         return S_OK;
  91.     }
  92.     
  93.     // last, check if the file exists in the media directory
  94.     _stprintf( strPath, _T("%s%s"), DXUtil_GetDXSDKMediaPath(), strShortName );
  95.  
  96.     file = CreateFile( strPath, GENERIC_READ, FILE_SHARE_READ, NULL, 
  97.                        OPEN_EXISTING, 0, NULL );
  98.     if( INVALID_HANDLE_VALUE != file )
  99.     {
  100.         CloseHandle( file );
  101.         return S_OK;
  102.     }
  103.  
  104.     // On failure, just return the file as the path
  105.     _tcscpy( strPath, strFilename );
  106.     return E_FAIL;
  107. }
  108.  
  109.  
  110.  
  111.  
  112. //-----------------------------------------------------------------------------
  113. // Name: DXUtil_ReadStringRegKey()
  114. // Desc: Helper function to read a registry key string
  115. //-----------------------------------------------------------------------------
  116. HRESULT DXUtil_ReadStringRegKey( HKEY hKey, TCHAR* strRegName, TCHAR* strValue, 
  117.                                  DWORD dwLength, TCHAR* strDefault )
  118. {
  119.     DWORD dwType;
  120.  
  121.     if( ERROR_SUCCESS != RegQueryValueEx( hKey, strRegName, 0, &dwType, 
  122.                                           (BYTE*)strValue, &dwLength ) )
  123.     {
  124.         _tcscpy( strValue, strDefault );
  125.     }
  126.  
  127.     return S_OK;
  128. }
  129.  
  130.  
  131.  
  132.  
  133. //-----------------------------------------------------------------------------
  134. // Name: DXUtil_WriteStringRegKey()
  135. // Desc: Helper function to write a registry key string
  136. //-----------------------------------------------------------------------------
  137. HRESULT DXUtil_WriteStringRegKey( HKEY hKey, TCHAR* strRegName,
  138.                                   TCHAR* strValue )
  139. {
  140.     if( ERROR_SUCCESS != RegSetValueEx( hKey, strRegName, 0, REG_SZ, 
  141.                                         (BYTE*)strValue, 
  142.                                         (_tcslen(strValue)+1)*sizeof(TCHAR) ) )
  143.         return E_FAIL;
  144.  
  145.     return S_OK;
  146. }
  147.  
  148.  
  149.  
  150.  
  151. //-----------------------------------------------------------------------------
  152. // Name: DXUtil_ReadIntRegKey()
  153. // Desc: Helper function to read a registry key int
  154. //-----------------------------------------------------------------------------
  155. HRESULT DXUtil_ReadIntRegKey( HKEY hKey, TCHAR* strRegName, DWORD* pdwValue, 
  156.                               DWORD dwDefault )
  157. {
  158.     DWORD dwType;
  159.     DWORD dwLength = sizeof(DWORD);
  160.  
  161.     if( ERROR_SUCCESS != RegQueryValueEx( hKey, strRegName, 0, &dwType, 
  162.                                           (BYTE*)pdwValue, &dwLength ) )
  163.     {
  164.         *pdwValue = dwDefault;
  165.     }
  166.  
  167.     return S_OK;
  168. }
  169.  
  170.  
  171.  
  172.  
  173. //-----------------------------------------------------------------------------
  174. // Name: DXUtil_WriteIntRegKey()
  175. // Desc: Helper function to write a registry key int
  176. //-----------------------------------------------------------------------------
  177. HRESULT DXUtil_WriteIntRegKey( HKEY hKey, TCHAR* strRegName, DWORD dwValue )
  178. {
  179.     if( ERROR_SUCCESS != RegSetValueEx( hKey, strRegName, 0, REG_DWORD, 
  180.                                         (BYTE*)&dwValue, sizeof(DWORD) ) )
  181.         return E_FAIL;
  182.  
  183.     return S_OK;
  184. }
  185.  
  186.  
  187.  
  188.  
  189. //-----------------------------------------------------------------------------
  190. // Name: DXUtil_ReadBoolRegKey()
  191. // Desc: Helper function to read a registry key BOOL
  192. //-----------------------------------------------------------------------------
  193. HRESULT DXUtil_ReadBoolRegKey( HKEY hKey, TCHAR* strRegName, BOOL* pbValue, 
  194.                               BOOL bDefault )
  195. {
  196.     DWORD dwType;
  197.     DWORD dwLength = sizeof(BOOL);
  198.  
  199.     if( ERROR_SUCCESS != RegQueryValueEx( hKey, strRegName, 0, &dwType, 
  200.                                           (BYTE*)pbValue, &dwLength ) )
  201.     {
  202.         *pbValue = bDefault;
  203.     }
  204.  
  205.     return S_OK;
  206. }
  207.  
  208.  
  209.  
  210.  
  211. //-----------------------------------------------------------------------------
  212. // Name: DXUtil_WriteBoolRegKey()
  213. // Desc: Helper function to write a registry key BOOL
  214. //-----------------------------------------------------------------------------
  215. HRESULT DXUtil_WriteBoolRegKey( HKEY hKey, TCHAR* strRegName, BOOL bValue )
  216. {
  217.     if( ERROR_SUCCESS != RegSetValueEx( hKey, strRegName, 0, REG_DWORD, 
  218.                                         (BYTE*)&bValue, sizeof(BOOL) ) )
  219.         return E_FAIL;
  220.  
  221.     return S_OK;
  222. }
  223.  
  224.  
  225.  
  226.  
  227. //-----------------------------------------------------------------------------
  228. // Name: DXUtil_ReadGuidRegKey()
  229. // Desc: Helper function to read a registry key guid
  230. //-----------------------------------------------------------------------------
  231. HRESULT DXUtil_ReadGuidRegKey( HKEY hKey, TCHAR* strRegName, GUID* pGuidValue, 
  232.                                GUID& guidDefault )
  233. {
  234.     DWORD dwType;
  235.     DWORD dwLength = sizeof(GUID);
  236.  
  237.     if( ERROR_SUCCESS != RegQueryValueEx( hKey, strRegName, 0, &dwType, 
  238.                                           (LPBYTE) pGuidValue, &dwLength ) )
  239.     {
  240.         *pGuidValue = guidDefault;
  241.     }
  242.  
  243.     return S_OK;
  244. }
  245.  
  246.  
  247.  
  248.  
  249. //-----------------------------------------------------------------------------
  250. // Name: DXUtil_WriteGuidRegKey()
  251. // Desc: Helper function to write a registry key guid
  252. //-----------------------------------------------------------------------------
  253. HRESULT DXUtil_WriteGuidRegKey( HKEY hKey, TCHAR* strRegName, GUID guidValue )
  254. {
  255.     if( ERROR_SUCCESS != RegSetValueEx( hKey, strRegName, 0, REG_BINARY, 
  256.                                         (BYTE*)&guidValue, sizeof(GUID) ) )
  257.         return E_FAIL;
  258.  
  259.     return S_OK;
  260. }
  261.  
  262.  
  263.  
  264.  
  265. //-----------------------------------------------------------------------------
  266. // Name: DXUtil_Timer()
  267. // Desc: Performs timer opertations. Use the following commands:
  268. //          TIMER_RESET           - to reset the timer
  269. //          TIMER_START           - to start the timer
  270. //          TIMER_STOP            - to stop (or pause) the timer
  271. //          TIMER_ADVANCE         - to advance the timer by 0.1 seconds
  272. //          TIMER_GETABSOLUTETIME - to get the absolute system time
  273. //          TIMER_GETAPPTIME      - to get the current time
  274. //          TIMER_GETELAPSEDTIME  - to get the time that elapsed between 
  275. //                                  TIMER_GETELAPSEDTIME calls
  276. //-----------------------------------------------------------------------------
  277. FLOAT __stdcall DXUtil_Timer( TIMER_COMMAND command )
  278. {
  279.     static BOOL     m_bTimerInitialized = FALSE;
  280.     static BOOL     m_bUsingQPF         = FALSE;
  281.     static BOOL     m_bTimerStopped     = TRUE;
  282.     static LONGLONG m_llQPFTicksPerSec  = 0;
  283.  
  284.     // Initialize the timer
  285.     if( FALSE == m_bTimerInitialized )
  286.     {
  287.         m_bTimerInitialized = TRUE;
  288.  
  289.         // Use QueryPerformanceFrequency() to get frequency of timer.  If QPF is
  290.         // not supported, we will timeGetTime() which returns milliseconds.
  291.         LARGE_INTEGER qwTicksPerSec;
  292.         m_bUsingQPF = QueryPerformanceFrequency( &qwTicksPerSec );
  293.         if( m_bUsingQPF )
  294.             m_llQPFTicksPerSec = qwTicksPerSec.QuadPart;
  295.     }
  296.  
  297.     if( m_bUsingQPF )
  298.     {
  299.         static LONGLONG m_llStopTime        = 0;
  300.         static LONGLONG m_llLastElapsedTime = 0;
  301.         static LONGLONG m_llBaseTime        = 0;
  302.         double fTime;
  303.         double fElapsedTime;
  304.         LARGE_INTEGER qwTime;
  305.         
  306.         // Get either the current time or the stop time, depending
  307.         // on whether we're stopped and what command was sent
  308.         if( m_llStopTime != 0 && command != TIMER_START && command != TIMER_GETABSOLUTETIME)
  309.             qwTime.QuadPart = m_llStopTime;
  310.         else
  311.             QueryPerformanceCounter( &qwTime );
  312.  
  313.         // Return the elapsed time
  314.         if( command == TIMER_GETELAPSEDTIME )
  315.         {
  316.             fElapsedTime = (double) ( qwTime.QuadPart - m_llLastElapsedTime ) / (double) m_llQPFTicksPerSec;
  317.             m_llLastElapsedTime = qwTime.QuadPart;
  318.             return (FLOAT) fElapsedTime;
  319.         }
  320.     
  321.         // Return the current time
  322.         if( command == TIMER_GETAPPTIME )
  323.         {
  324.             double fAppTime = (double) ( qwTime.QuadPart - m_llBaseTime ) / (double) m_llQPFTicksPerSec;
  325.             return (FLOAT) fAppTime;
  326.         }
  327.     
  328.         // Reset the timer
  329.         if( command == TIMER_RESET )
  330.         {
  331.             m_llBaseTime        = qwTime.QuadPart;
  332.             m_llLastElapsedTime = qwTime.QuadPart;
  333.             m_llStopTime        = 0;
  334.             m_bTimerStopped     = FALSE;
  335.             return 0.0f;
  336.         }
  337.     
  338.         // Start the timer
  339.         if( command == TIMER_START )
  340.         {
  341.             if( m_bTimerStopped )
  342.                 m_llBaseTime += qwTime.QuadPart - m_llStopTime;
  343.             m_llStopTime = 0;
  344.             m_llLastElapsedTime = qwTime.QuadPart;
  345.             m_bTimerStopped = FALSE;
  346.             return 0.0f;
  347.         }
  348.     
  349.         // Stop the timer
  350.         if( command == TIMER_STOP )
  351.         {
  352.             m_llStopTime = qwTime.QuadPart;
  353.             m_llLastElapsedTime = qwTime.QuadPart;
  354.             m_bTimerStopped = TRUE;
  355.             return 0.0f;
  356.         }
  357.     
  358.         // Advance the timer by 1/10th second
  359.         if( command == TIMER_ADVANCE )
  360.         {
  361.             m_llStopTime += m_llQPFTicksPerSec/10;
  362.             return 0.0f;
  363.         }
  364.  
  365.         if( command == TIMER_GETABSOLUTETIME )
  366.         {
  367.             fTime = qwTime.QuadPart / (double) m_llQPFTicksPerSec;
  368.             return (FLOAT) fTime;
  369.         }
  370.  
  371.         return -1.0f; // Invalid command specified
  372.     }
  373.     else
  374.     {
  375.         // Get the time using timeGetTime()
  376.         static double m_fLastElapsedTime  = 0.0;
  377.         static double m_fBaseTime         = 0.0;
  378.         static double m_fStopTime         = 0.0;
  379.         double fTime;
  380.         double fElapsedTime;
  381.         
  382.         // Get either the current time or the stop time, depending
  383.         // on whether we're stopped and what command was sent
  384.         if( m_fStopTime != 0.0 && command != TIMER_START && command != TIMER_GETABSOLUTETIME)
  385.             fTime = m_fStopTime;
  386.         else
  387.             fTime = timeGetTime() * 0.001;
  388.     
  389.         // Return the elapsed time
  390.         if( command == TIMER_GETELAPSEDTIME )
  391.         {   
  392.             fElapsedTime = (double) (fTime - m_fLastElapsedTime);
  393.             m_fLastElapsedTime = fTime;
  394.             return (FLOAT) fElapsedTime;
  395.         }
  396.     
  397.         // Return the current time
  398.         if( command == TIMER_GETAPPTIME )
  399.         {
  400.             return (FLOAT) (fTime - m_fBaseTime);
  401.         }
  402.     
  403.         // Reset the timer
  404.         if( command == TIMER_RESET )
  405.         {
  406.             m_fBaseTime         = fTime;
  407.             m_fLastElapsedTime  = fTime;
  408.             m_fStopTime         = 0;
  409.             m_bTimerStopped     = FALSE;
  410.             return 0.0f;
  411.         }
  412.     
  413.         // Start the timer
  414.         if( command == TIMER_START )
  415.         {
  416.             if( m_bTimerStopped )
  417.                 m_fBaseTime += fTime - m_fStopTime;
  418.             m_fStopTime = 0.0f;
  419.             m_fLastElapsedTime  = fTime;
  420.             m_bTimerStopped = FALSE;
  421.             return 0.0f;
  422.         }
  423.     
  424.         // Stop the timer
  425.         if( command == TIMER_STOP )
  426.         {
  427.             m_fStopTime = fTime;
  428.             m_fLastElapsedTime  = fTime;
  429.             m_bTimerStopped = TRUE;
  430.             return 0.0f;
  431.         }
  432.     
  433.         // Advance the timer by 1/10th second
  434.         if( command == TIMER_ADVANCE )
  435.         {
  436.             m_fStopTime += 0.1f;
  437.             return 0.0f;
  438.         }
  439.  
  440.         if( command == TIMER_GETABSOLUTETIME )
  441.         {
  442.             return (FLOAT) fTime;
  443.         }
  444.  
  445.         return -1.0f; // Invalid command specified
  446.     }
  447. }
  448.  
  449.  
  450.  
  451.  
  452. //-----------------------------------------------------------------------------
  453. // Name: DXUtil_ConvertAnsiStringToWide()
  454. // Desc: This is a UNICODE conversion utility to convert a CHAR string into a
  455. //       WCHAR string. cchDestChar defaults -1 which means it 
  456. //       assumes strDest is large enough to store strSource
  457. //-----------------------------------------------------------------------------
  458. VOID DXUtil_ConvertAnsiStringToWide( WCHAR* wstrDestination, const CHAR* strSource, 
  459.                                      int cchDestChar )
  460. {
  461.     if( wstrDestination==NULL || strSource==NULL )
  462.         return;
  463.  
  464.     if( cchDestChar == -1 )
  465.         cchDestChar = strlen(strSource)+1;
  466.  
  467.     MultiByteToWideChar( CP_ACP, 0, strSource, -1, 
  468.                          wstrDestination, cchDestChar-1 );
  469.  
  470.     wstrDestination[cchDestChar-1] = 0;
  471. }
  472.  
  473.  
  474.  
  475.  
  476. //-----------------------------------------------------------------------------
  477. // Name: DXUtil_ConvertWideStringToAnsi()
  478. // Desc: This is a UNICODE conversion utility to convert a WCHAR string into a
  479. //       CHAR string. cchDestChar defaults -1 which means it 
  480. //       assumes strDest is large enough to store strSource
  481. //-----------------------------------------------------------------------------
  482. VOID DXUtil_ConvertWideStringToAnsi( CHAR* strDestination, const WCHAR* wstrSource, 
  483.                                      int cchDestChar )
  484. {
  485.     if( strDestination==NULL || wstrSource==NULL )
  486.         return;
  487.  
  488.     if( cchDestChar == -1 )
  489.         cchDestChar = wcslen(wstrSource)+1;
  490.  
  491.     WideCharToMultiByte( CP_ACP, 0, wstrSource, -1, strDestination, 
  492.                          cchDestChar-1, NULL, NULL );
  493.  
  494.     strDestination[cchDestChar-1] = 0;
  495. }
  496.  
  497.  
  498.  
  499.  
  500. //-----------------------------------------------------------------------------
  501. // Name: DXUtil_ConvertGenericStringToAnsi()
  502. // Desc: This is a UNICODE conversion utility to convert a TCHAR string into a
  503. //       CHAR string. cchDestChar defaults -1 which means it 
  504. //       assumes strDest is large enough to store strSource
  505. //-----------------------------------------------------------------------------
  506. VOID DXUtil_ConvertGenericStringToAnsi( CHAR* strDestination, const TCHAR* tstrSource, 
  507.                                         int cchDestChar )
  508. {
  509.     if( strDestination==NULL || tstrSource==NULL || cchDestChar == 0 )
  510.         return;
  511.  
  512. #ifdef _UNICODE
  513.     DXUtil_ConvertWideStringToAnsi( strDestination, tstrSource, cchDestChar );
  514. #else
  515.     if( cchDestChar == -1 )
  516.     {
  517.         strcpy( strDestination, tstrSource );
  518.     }
  519.     else
  520.     {
  521.         strncpy( strDestination, tstrSource, cchDestChar );
  522.         strDestination[cchDestChar-1] = '\0';
  523.     }
  524. #endif
  525. }
  526.  
  527.  
  528.  
  529.  
  530. //-----------------------------------------------------------------------------
  531. // Name: DXUtil_ConvertGenericStringToWide()
  532. // Desc: This is a UNICODE conversion utility to convert a TCHAR string into a
  533. //       WCHAR string. cchDestChar defaults -1 which means it 
  534. //       assumes strDest is large enough to store strSource
  535. //-----------------------------------------------------------------------------
  536. VOID DXUtil_ConvertGenericStringToWide( WCHAR* wstrDestination, const TCHAR* tstrSource, 
  537.                                         int cchDestChar )
  538. {
  539.     if( wstrDestination==NULL || tstrSource==NULL || cchDestChar == 0 )
  540.         return;
  541.  
  542. #ifdef _UNICODE
  543.     if( cchDestChar == -1 )
  544.     {
  545.         wcscpy( wstrDestination, tstrSource );
  546.     }
  547.     else
  548.     {
  549.         wcsncpy( wstrDestination, tstrSource, cchDestChar );
  550.         wstrDestination[cchDestChar-1] = L'\0';
  551.     }
  552. #else
  553.     DXUtil_ConvertAnsiStringToWide( wstrDestination, tstrSource, cchDestChar );
  554. #endif
  555. }
  556.  
  557.  
  558.  
  559.  
  560. //-----------------------------------------------------------------------------
  561. // Name: DXUtil_ConvertAnsiStringToGeneric()
  562. // Desc: This is a UNICODE conversion utility to convert a CHAR string into a
  563. //       TCHAR string. cchDestChar defaults -1 which means it 
  564. //       assumes strDest is large enough to store strSource
  565. //-----------------------------------------------------------------------------
  566. VOID DXUtil_ConvertAnsiStringToGeneric( TCHAR* tstrDestination, const CHAR* strSource, 
  567.                                         int cchDestChar )
  568. {
  569.     if( tstrDestination==NULL || strSource==NULL || cchDestChar == 0 )
  570.         return;
  571.         
  572. #ifdef _UNICODE
  573.     DXUtil_ConvertAnsiStringToWide( tstrDestination, strSource, cchDestChar );
  574. #else
  575.     if( cchDestChar == -1 )
  576.     {
  577.         strcpy( tstrDestination, strSource );
  578.     }
  579.     else
  580.     {
  581.         strncpy( tstrDestination, strSource, cchDestChar );
  582.         tstrDestination[cchDestChar-1] = '\0';
  583.     }
  584. #endif
  585. }
  586.  
  587.  
  588.  
  589.  
  590. //-----------------------------------------------------------------------------
  591. // Name: DXUtil_ConvertAnsiStringToGeneric()
  592. // Desc: This is a UNICODE conversion utility to convert a WCHAR string into a
  593. //       TCHAR string. cchDestChar defaults -1 which means it 
  594. //       assumes strDest is large enough to store strSource
  595. //-----------------------------------------------------------------------------
  596. VOID DXUtil_ConvertWideStringToGeneric( TCHAR* tstrDestination, const WCHAR* wstrSource, 
  597.                                         int cchDestChar )
  598. {
  599.     if( tstrDestination==NULL || wstrSource==NULL || cchDestChar == 0 )
  600.         return;
  601.  
  602. #ifdef _UNICODE
  603.     if( cchDestChar == -1 )
  604.     {
  605.         wcscpy( tstrDestination, wstrSource );
  606.     }
  607.     else
  608.     {
  609.         wcsncpy( tstrDestination, wstrSource, cchDestChar );
  610.         tstrDestination[cchDestChar-1] = L'\0';
  611.     }
  612. #else
  613.     DXUtil_ConvertWideStringToAnsi( tstrDestination, wstrSource, cchDestChar );
  614. #endif
  615. }
  616.  
  617.  
  618.  
  619.  
  620. //-----------------------------------------------------------------------------
  621. // Name: _DbgOut()
  622. // Desc: Outputs a message to the debug stream
  623. //-----------------------------------------------------------------------------
  624. HRESULT _DbgOut( TCHAR* strFile, DWORD dwLine, HRESULT hr, TCHAR* strMsg )
  625. {
  626.     TCHAR buffer[256];
  627.     wsprintf( buffer, _T("%s(%ld): "), strFile, dwLine );
  628.     OutputDebugString( buffer );
  629.     OutputDebugString( strMsg );
  630.  
  631.     if( hr )
  632.     {
  633.         wsprintf( buffer, _T("(hr=%08lx)\n"), hr );
  634.         OutputDebugString( buffer );
  635.     }
  636.  
  637.     OutputDebugString( _T("\n") );
  638.  
  639.     return hr;
  640. }
  641.  
  642.  
  643.  
  644.  
  645. //-----------------------------------------------------------------------------
  646. // Name: DXUtil_Trace()
  647. // Desc: Outputs to the debug stream a formatted string with a variable-
  648. //       argument list.
  649. //-----------------------------------------------------------------------------
  650. VOID DXUtil_Trace( TCHAR* strMsg, ... )
  651. {
  652. #if defined(DEBUG) | defined(_DEBUG)
  653.     TCHAR strBuffer[512];
  654.     
  655.     va_list args;
  656.     va_start(args, strMsg);
  657.     _vsntprintf( strBuffer, 512, strMsg, args );
  658.     va_end(args);
  659.  
  660.     OutputDebugString( strBuffer );
  661. #else
  662.     UNREFERENCED_PARAMETER(strMsg);
  663. #endif
  664. }
  665.  
  666.  
  667.  
  668.  
  669. //-----------------------------------------------------------------------------
  670. // Name: DXUtil_ConvertStringToGUID()
  671. // Desc: Converts a string to a GUID
  672. //-----------------------------------------------------------------------------
  673. BOOL DXUtil_ConvertStringToGUID( const TCHAR* strIn, GUID* pGuidOut )
  674. {
  675.     UINT aiTmp[10];
  676.  
  677.     if( _stscanf( strIn, TEXT("{%8X-%4X-%4X-%2X%2X-%2X%2X%2X%2X%2X%2X}"),
  678.                     &pGuidOut->Data1, 
  679.                     &aiTmp[0], &aiTmp[1], 
  680.                     &aiTmp[2], &aiTmp[3],
  681.                     &aiTmp[4], &aiTmp[5],
  682.                     &aiTmp[6], &aiTmp[7],
  683.                     &aiTmp[8], &aiTmp[9] ) != 11 )
  684.     {
  685.         ZeroMemory( pGuidOut, sizeof(GUID) );
  686.         return FALSE;
  687.     }
  688.     else
  689.     {
  690.         pGuidOut->Data2       = (USHORT) aiTmp[0];
  691.         pGuidOut->Data3       = (USHORT) aiTmp[1];
  692.         pGuidOut->Data4[0]    = (BYTE) aiTmp[2];
  693.         pGuidOut->Data4[1]    = (BYTE) aiTmp[3];
  694.         pGuidOut->Data4[2]    = (BYTE) aiTmp[4];
  695.         pGuidOut->Data4[3]    = (BYTE) aiTmp[5];
  696.         pGuidOut->Data4[4]    = (BYTE) aiTmp[6];
  697.         pGuidOut->Data4[5]    = (BYTE) aiTmp[7];
  698.         pGuidOut->Data4[6]    = (BYTE) aiTmp[8];
  699.         pGuidOut->Data4[7]    = (BYTE) aiTmp[9];
  700.         return TRUE;
  701.     }
  702. }
  703.  
  704.  
  705.  
  706.  
  707. //-----------------------------------------------------------------------------
  708. // Name: DXUtil_ConvertGUIDToString()
  709. // Desc: Converts a GUID to a string 
  710. //-----------------------------------------------------------------------------
  711. VOID DXUtil_ConvertGUIDToString( const GUID* pGuidIn, TCHAR* strOut )
  712. {
  713.     _stprintf( strOut, TEXT("{%0.8X-%0.4X-%0.4X-%0.2X%0.2X-%0.2X%0.2X%0.2X%0.2X%0.2X%0.2X}"),
  714.                pGuidIn->Data1, pGuidIn->Data2, pGuidIn->Data3,
  715.                pGuidIn->Data4[0], pGuidIn->Data4[1],
  716.                pGuidIn->Data4[2], pGuidIn->Data4[3],
  717.                pGuidIn->Data4[4], pGuidIn->Data4[5],
  718.                pGuidIn->Data4[6], pGuidIn->Data4[7] );
  719. }
  720.